<template>
  <div>
    <el-row class="top" style="height: 160px">
      <!-- 商城logo -->
      <div style="float: left">
        <img src="../assets/logo.jpg" alt="" style="height: 150px" />
      </div>
      <!-- 搜索框 -->
      <div style="float: left; margin-left: 5%">
        <el-input
          placeholder="请输入内容"
          style="width: 400px; line-height: 150px"
        ></el-input>
        <!-- 搜索按钮 -->
        <el-button type="danger" icon="el-icon-search"></el-button>
        <!-- 购物车 -->
        <el-badge :value="0" class="item">
          <el-button
            icon="el-icon-shopping-cart-full"
            style="margin-left: 10%"
            @click="gocart"
            >购物车</el-button
          >
        </el-badge>
      </div>
    </el-row>
    <el-row class="layout">
      <!-- 轮播图 -->
      <el-carousel :interval="4000" height="500px">
        <el-carousel-item v-for="item in imagebox" :key="item.id">
          <img :src="item.idView" class="image" width="100%" height="inherit" />
        </el-carousel-item>
      </el-carousel>
      <!-- 电器 -->
      <h1>电器</h1>
      <el-card style="width: 100%">
        <!-- 循环！！ -->
        <el-card class="goods" v-for="item in clothesBoy" :key="item.id">
          <el-link
            @click="goodsDetails(item.id)"
            target="_blank"
            :underline="false"
          >
            <img :src="item.goodsImage" width="100%" height="180px" alt="" />
            <br />
            <span>{{ item.goodsName }} </span>
            <br /><br /><span style="color: red">¥{{ item.newPrice }}</span>
          </el-link>
        </el-card>
      </el-card>
      <!-- 洗护 -->
      <h1>洗护</h1>
      <el-card>
        <!-- 循环！！ -->
        <el-card class="goods" v-for="item in clothesGirl" :key="item.id">
          <el-link
            @click="goodsDetails(item.id)"
            target="_blank"
            :underline="false"
          >
            <img :src="item.goodsImage" width="100%" height="180px" alt="" />
            <br />
            <span>{{ item.goodsName }} </span>
            <br /><br /><span style="color: red">¥{{ item.newPrice }}</span>
          </el-link>
        </el-card>
      </el-card>
      <!-- 居家 -->
      <h1>居家</h1>
      <el-card>
        <!-- 循环！！ -->
        <el-card class="goods" v-for="item in clothesOther" :key="item.id">
          <el-link
            @click="goodsDetails(item.id)"
            target="_blank"
            :underline="false"
          >
            <img :src="item.goodsImage" width="100%" height="180px" alt="" />
            <br />
            <span>{{ item.goodsName }} </span>
            <br /><br /><span style="color: red">¥{{ item.newPrice }}</span>
          </el-link>
        </el-card>
      </el-card>
    </el-row>
  </div>
</template>

<script>
import { Goods } from "../api/goods";

export default {
  data() {
    return {
      imagebox: [
        { id: 0, idView: require("../assets/家居1.png") },
        { id: 1, idView: require("../assets/家居2.png") },
        { id: 2, idView: require("../assets/家居3.png") },
      ],
      screenWidth: 0,
      clothesBoy: [],
      clothesGirl: [],
      clothesOther: [],
    };
  },
  methods: {
    setSize: function () {
      // 通过浏览器宽度(图片宽度)计算高度
      this.bannerHeight = (400 / 1920) * this.screenWidth;
    },
    gocart() {
      this.$router.push("/cart");
    },
    goodsDetails(id) {
      // console.log(id);
      //获取对应商品详情
      this.$router.push(`/details?id=${id}`);
    },
  },
  mounted() {
    // 首次加载时,需要调用一次
    this.screenWidth = window.innerWidth;
    this.setSize();
    // 窗口大小发生改变时,调用一次
    window.onresize = () => {
      this.screenWidth = window.innerWidth;
      this.setSize();
    };
    Goods().then(({ data }) => {
      // console.log(data);
      var AllGoods = data.data.data;
      // console.log(AllGoods);
      AllGoods.forEach((item) => {
        item.goodsImage = this.$host + item.goodsImage;
      });

      AllGoods.forEach((item) => {
        // console.log(item.category2Name);
        if (item.category1Name === "家居生活") {
          // console.log("22222");
          if (item.category2Name === "电器") {
            // console.log("1");
            this.clothesBoy.push(item);
          } else if (item.category2Name === "洗护") {
            this.clothesGirl.push(item);
          } else {
            this.clothesOther.push(item);
          }
        }
      });
      // console.log(this.clothesGirl);
    });
  },
};
</script>

<style>
.top {
  width: 60%;
  margin: auto;
}
body {
  background-color: white;
  min-height: calc(100vh - 120px);
}
.layout {
  width: 60%;
  margin: auto;
  min-height: calc(100vh - 120px);
  background-color: #e9eef3;
}
/* 商品 */
.goods {
  width: 24%;
  height: 320px;
  float: left;
  margin-left: 0.5%;
  margin-top: 1%;
}
h1 {
  text-align: center;
  /* margin-top:15px; */
}
</style>